﻿//---------------------------------------------------------------------------
// <summary>
// Represents the Event Log Information section of an Event Log
// </summary>
//---------------------------------------------------------------------------

namespace InfoPathExtractionRules
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;

    /// <summary>
    /// Enumerates the indexes of the data that is sent in a postback body.
    /// PostBack body items are separated by semicolons.
    /// </summary>
    public enum EventLogInformationIndex
    {
        /// <summary>
        /// Index of the PostBack Counter item.
        /// </summary>
        PostBackCounter = 1,

        /// <summary>
        /// Index of the Editing Session ID item.
        /// </summary>
        EditingSessionId = 2,

        /// <summary>
        /// Index of the Solution ID item.
        /// </summary>
        SolutionId = 3,

        /// <summary>
        /// Index of the Session State item.
        /// </summary>
        SessionState = 18,

        /// <summary>
        /// Index of the Form ID item.
        /// </summary>
        FormId = 19,

        /// <summary>
        /// Index of the Canary Value item.
        /// </summary>
        CanaryValue = 25,

        /// <summary>
        /// Number of Items that event log info should have.
        /// </summary>
        NumberOfItems = 26
    }

    /// <summary>
    /// Represents the first half of an EventLog.
    /// Contains metadata about the form that is being edited.
    /// </summary>
    public class EventLogInformation
    {
        /// <summary>
        /// The items of meta data.
        /// </summary>
        private string[] items;

        /// <summary>
        /// Initializes a new instance of the EventLogInformation class.
        /// </summary>
        /// <param name="eventLogInfo">String of items separated by semicolons.</param>
        public EventLogInformation(string eventLogInfo)
        {
            this.items = eventLogInfo.Split(EventLog.EventLogItemSeparator);
            int numberOfItems = this.items.Length;
            if (numberOfItems != (int)EventLogInformationIndex.NumberOfItems)
            {
                string error =
                    string.Format(
                    System.Globalization.CultureInfo.CurrentCulture,
                    "Event log info has {0} items, expected {1}",
                    numberOfItems,
                    (int)EventLogInformationIndex.NumberOfItems);
                Trace.TraceError(error);
            }
        }

        /// <summary>
        /// Gets or sets the number of times the form has performed postbacks.
        /// </summary>
        /// <value>
        /// Number of times the form has performed postbacks.
        /// </value>
        public int PostBackCounter
        {
            get { return int.Parse(this.items[(int)EventLogInformationIndex.PostBackCounter], System.Globalization.NumberFormatInfo.InvariantInfo); }
            set { this.items[(int)EventLogInformationIndex.PostBackCounter] = value.ToString(System.Globalization.NumberFormatInfo.InvariantInfo); }
        }

        /// <summary>
        /// Gets or sets the key that identifies a browser's session.
        /// </summary>
        /// <value>
        /// Key that identifies a browser's session.
        /// </value>
        public string EditingSessionId
        {
            get { return this.items[(int)EventLogInformationIndex.EditingSessionId]; }
            set { this.items[(int)EventLogInformationIndex.EditingSessionId] = value; }
        }

        /// <summary>
        /// Gets or sets the key that identifies the form template that was opened.
        /// </summary>
        /// <value>
        /// Key that identifies the form template that was opened.
        /// </value>
        public string SolutionId
        {
            get { return this.items[(int)EventLogInformationIndex.SolutionId]; }
            set { this.items[(int)EventLogInformationIndex.SolutionId] = value; }
        }

        /// <summary>
        /// Gets or sets the key that identifies the session state.
        /// </summary>
        /// <value>
        /// Key that identifies the session state.
        /// </value>
        public string SessionState
        {
            get { return this.items[(int)EventLogInformationIndex.SessionState]; }
            set { this.items[(int)EventLogInformationIndex.SessionState] = value; }
        }

        /// <summary>
        /// Gets or sets the security mitigation for playback attacks.
        /// This value changes after every postback.
        /// </summary>
        /// <value>
        /// Security mitigation for playback attacks.
        /// This value changes after every postback.
        /// </value>
        public string Canary
        {
            get { return this.items[(int)EventLogInformationIndex.CanaryValue]; }
            set { this.items[(int)EventLogInformationIndex.CanaryValue] = value; }
        }

        /// <summary>
        /// Gets or sets the key that identifies a form instance on a web page.
        /// Used by webparts.
        /// </summary>
        /// <value>
        /// Key that identifies a form instance on a web page.
        /// Used by webparts.
        /// </value>
        public string FormId
        {
            get { return this.items[(int)EventLogInformationIndex.FormId]; }
            set { this.items[(int)EventLogInformationIndex.FormId] = value; }
        }

        /// <summary>
        /// Builds the event log info.
        /// </summary>
        /// <returns>All the EventLogInfo items joined together with a semicolon.</returns>
        public override string ToString()
        {
            return String.Join(EventLog.EventLogItemSeparator.ToString(), this.items);
        }
    }
}
